home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Connection Tool ƒ / Scripting.c < prev    next >
C/C++ Source or Header  |  1994-02-19  |  7KB  |  247 lines

  1. // ===========================================================================
  2. // "Connection Tool Skeleton in C" for the Communications Toolbox
  3. // 
  4. //    Copyright © 1994 Peter J. Creath
  5. //    All Rights Reserved Worldwide
  6. // ===========================================================================
  7.  
  8. #include "ConnToolCommon.h"
  9.  
  10. // ===========================================================================
  11. // Function prototypes
  12. // ===========================================================================
  13. extern pascal long    main(ConnHandle hConn, short msg, Ptr pConfig, long unused, long unused2);
  14. extern Ptr        GetConfig(ConnHandle hConn);
  15. extern short        SetConfig(ConnHandle hConn, Ptr theSource);
  16. extern void        CStrCopy(char *sourceStr, char *targetStr);
  17. extern void        CStrAppend(char *targetStr, char *sourceStr);
  18. extern void        P2CStrConvert(char *sourceStr, char *targetStr);
  19. extern void        C2PStrConvert(char *sourceStr, char *targetStr);
  20. extern short        CStrLen(char *sourceStr);
  21. extern void        GetToken(char **sourceStr, char *targetStr, char delim);
  22. extern Boolean    CStrEqual(char *sourceStr, char *targetStr);
  23.  
  24. // ===========================================================================
  25. // main()
  26. //     This function is the entry point for the 'cscr' resource.  It passes control to the appropriate
  27. //     subroutines, depending on the incoming message.  This can probably remain unchanged.
  28. // ===========================================================================
  29. pascal long main(ConnHandle hConn, short msg, Ptr pConfig, long unused, long unused2)
  30. {
  31. long            rtnValue;
  32.  
  33.     switch (msg)
  34.         {
  35.             case cmMgetMsg:                                        
  36.                 rtnValue = (long)GetConfig(hConn);
  37.                 break;
  38.             
  39.             case cmMsetMsg:                                        
  40.                 rtnValue = SetConfig(hConn, pConfig);
  41.                 break;
  42.  
  43.             default:                                                        // Say WHAT?
  44.                 rtnValue = cmNotSupported;
  45.                 break;
  46.         }
  47. }
  48.  
  49. // ===========================================================================
  50. // GetConfig()
  51. //     This function is called in response to a cmMgetMsg.  It should return a string that describes the
  52. //     connection record.
  53. // ===========================================================================
  54. Ptr    GetConfig(ConnHandle hConn)
  55. {
  56. Ptr            thePtr;
  57. ConfigPtr    pConfig;
  58. Str255        theString, string2;
  59. short        len;
  60.  
  61.     pConfig = (ConfigPtr)((*hConn)->config);                    // Get the config record
  62.     CStrCopy("FOOBAR", (char *)theString);                    // The attribute's name is FOOBAR
  63.     NumToString(pConfig->foobar, string2);                    // Get the attribute value
  64.     P2CStrConvert((char *)string2, (char *)string2);    // Convert to C string
  65.     CStrAppend((char *)theString, (char *)string2);        // Concatenate string2 to theString
  66.     len = CStrLen((char *)theString) + 1;                        // Get its length (incl. trailing null)
  67.     thePtr = NewPtr(len);                                                // Allocate space for it
  68.     
  69.     if (thePtr)
  70.         {
  71.             BlockMove(theString, thePtr, len);
  72.         }
  73.     
  74.     return (thePtr);
  75. }
  76.  
  77.  
  78. // ===========================================================================
  79. // SetConfig()
  80. //     This function is called in response to a cmMsetMsg.  It should set the specific configuration
  81. //    fields requested.  It should return 0 for no error, -1 for a generic error, a number < -1 for an
  82. //    OSErr, and a positive number for the character position where the parsing stopped.
  83. // ===========================================================================
  84. short    SetConfig(ConnHandle hConn, Ptr theSource)
  85. {
  86. ConfigPtr    pConfig;
  87. Str255        paramStr, valueStr;
  88. Boolean        outOfTokens;
  89. short        rtnValue;
  90. long            localLong;
  91. char            *tokenStart, *sourceStart;
  92.  
  93.     pConfig = (ConfigPtr)((*hConn)->config);
  94.     rtnValue = noErr;
  95.     sourceStart = theSource;
  96.     
  97.     if (*theSource == 0)
  98.         {
  99.             outOfTokens = TRUE;
  100.         }
  101.     else
  102.         {
  103.             outOfTokens = FALSE;
  104.         }
  105.  
  106. //    This currently only parses strings using space delimiters.  It is not WorldScript-savvy™.  If
  107. //    you want to add that support (using IntlTokenize), you'll sacrifice System 6.0.x support.  I
  108. //    really don't see any point in doing that until OTI supercedes CTB.  (OpenTransport Interface, and
  109. //    Communications ToolBox, respectively)
  110.  
  111.     while (!outOfTokens)
  112.         {
  113.             tokenStart = theSource;
  114.             GetToken(&theSource, (char *)paramStr, ' ');
  115.             if (CStrEqual((char *)paramStr, "FOOBAR"))
  116.                 {
  117.                     GetToken(&theSource, (char *)paramStr, ' ');
  118.                     C2PStrConvert((char *)paramStr, (char *)valueStr);
  119.                     StringToNum(valueStr, &localLong);
  120.                     pConfig->foobar = localLong;
  121.                 }
  122.             else
  123.                 {
  124.                     rtnValue = (tokenStart - sourceStart) + 1;
  125.                     outOfTokens = TRUE;
  126.                 }
  127.         }
  128.  
  129.     return (rtnValue);
  130. }
  131.  
  132. // ===========================================================================
  133. // These may not be pretty, but they were designed to compile as efficiently as possible under Think C
  134. // 6.  (Try disassembling them...I don't think you'll get them much tighter)
  135. //
  136. // Excerpted from
  137. //
  138. // "StrUtils.c" hand-coded 680x0 assembly library
  139. // Translated to C to compile to code identical to the hand-coded asm
  140. // Copyright © 1992-1994 Peter J. Creath
  141. // All Rights Reserved Worldwide
  142. // ===========================================================================
  143.  
  144. void    CStrCopy(char *sourceStr, char *targetStr)
  145. {
  146. register char    *pSource;
  147. register char    *pTarget;
  148.  
  149.     pSource    = sourceStr;
  150.     pTarget        = targetStr;
  151.  
  152.     while (*(pTarget++) = *(pSource++));
  153. }
  154.  
  155. void CStrAppend(char *targetStr, char *sourceStr)
  156. {
  157. register char    *pSource;
  158. register char    *pTarget;
  159.  
  160.     pSource    = sourceStr;
  161.     pTarget        = targetStr;
  162.  
  163.     while (*(pTarget++));                                // Move to end of C string
  164.     pTarget--;                                                // Move back to erase trailing null
  165.     while (*(pTarget++) = *(pSource++));        // Copy to this new point
  166. }
  167.  
  168. void    P2CStrConvert(char *sourceStr, char *targetStr)
  169. {
  170. register char    *pSource;
  171. register char    *pTarget;
  172. register char    len;
  173.  
  174.     pSource    = sourceStr;
  175.     pTarget        = targetStr;
  176.  
  177.     if (len = *(pSource)++)                                // Get length of Pascal string
  178.         {
  179.             do
  180.                 {
  181.                     *(pTarget++) = *(pSource++);    // Copy string
  182.                 } while (--len);
  183.         }
  184.     *pTarget = 0;                                            // Add trailing null
  185. }
  186.  
  187. void    C2PStrConvert(char *sourceStr, char *targetStr)
  188. {
  189. register char    *pSource;
  190. register char    *pTarget;
  191.  
  192.     pSource    = sourceStr;
  193.     pTarget        = targetStr + 1;
  194.  
  195.     while (*(pTarget++) = *(pSource++));
  196.  
  197.     pTarget        = targetStr;
  198.     *pTarget    = (pSource - sourceStr - 1);
  199. }
  200.  
  201. short    CStrLen(char *sourceStr)
  202. {
  203. register char    *pSource;
  204. register char    len = 0;
  205.  
  206.     pSource = sourceStr;
  207.     while (*(pSource++)) len++;
  208.     
  209.     return (len);
  210. }
  211.  
  212. void        GetToken(char **sourceStr, char *targetStr, char delim)
  213. {
  214. register char    *pSource;
  215. register char    *pTarget;
  216. register char    locDelim;
  217.  
  218.     pSource = *sourceStr;
  219.     pTarget = targetStr;
  220.     locDelim = delim;
  221.     while (*pSource && *pSource != locDelim) *(pTarget++) = *(pSource++);
  222.     *pTarget = 0;
  223.     *sourceStr = pSource + 1;
  224. }
  225.  
  226. Boolean        CStrEqual(char *sourceStr, char *targetStr)
  227. {
  228. register char        *pSource;
  229. register char        *pTarget;
  230. register Boolean    areEqual;
  231.  
  232.     pSource = sourceStr;
  233.     pTarget = targetStr;
  234.     areEqual = TRUE;
  235.  
  236.     do
  237.         {
  238.             if (*pSource != *(pTarget++))
  239.                 {
  240.                     areEqual = FALSE;
  241.                     break;
  242.                 }
  243.         } while (*(pSource++));
  244.     
  245.     return (areEqual);
  246. }
  247.